package com.example.controller;

import com.example.exception.DataUploadException;
import com.example.info.SampleInfo;
import com.example.info.DeviceInfo;
import com.example.pojo.PageResult;
import com.example.pojo.Result;
import com.example.pojo.Sample;
import com.example.service.DeviceDataService;
import com.example.service.SampleService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static jdk.nashorn.internal.runtime.regexp.joni.Config.log;

@Slf4j
@RestController
@RequestMapping
public class DeviceController {
    @Autowired
    private DeviceDataService dataService;
    @Autowired
    private SampleService sampleService;



    /**
     * 上传图片    http://localhost:8881/post/data
     * @param file
     * @param json
     * @return
     */
    @PostMapping(value = "/post/data", consumes = "multipart/form-data")
    public ResponseEntity<Map<String, String>> uploadImage(
            @RequestPart("file") MultipartFile file,
            @RequestPart("json") String json) throws JsonProcessingException {
        log.info("正在上传");
        log.info(json);
                ObjectMapper mapper = new ObjectMapper();
                Sample sample= mapper.readValue(json, Sample.class);
        Map<String, String> response = new HashMap<>();
        try {
            String filePath = dataService.uploadImage(file);
            sample.setFileName(filePath);
            //System.out.println("soooooo"+sample);
            sampleService.savem(sample);
            response.put("status", "success");
            response.put("path", filePath);
            return ResponseEntity.ok(response);

        } catch (DataUploadException e) {
            response.put("error", e.getMessage());
            return ResponseEntity.badRequest().body(response);

        } catch (IOException e) {
            response.put("error", "文件保存失败: " + e.getMessage());
            return ResponseEntity.internalServerError().body(response);
        }
    }

    /**
     * 获取设备信息  --GET http://localhost:8881/device/data
     *
     * @return
     */
    @GetMapping("/device/data")
    public Result list(){
        log.info("获取设备信息");
        List<DeviceInfo> deviceInfoList = dataService.getData();
        return Result.success(deviceInfoList);
    }


    /**
     * 新增设备信息 --POST http://localhost:8881/device/data
     */
    @PostMapping("/device/data")
    public Result save(@RequestBody DeviceInfo deviceInfo){
        log.info("新增设备信息:{}",deviceInfo);
        dataService.save(deviceInfo);
        return Result.success();
    }

    /**
     * 分页获取所有设备信息 --GET http://localhost:8881/device/datas?page=1&pageSize=10
     */
    @GetMapping("/device/datas")
    public Result page(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize){
        log.info("分页获取所有设备信息");
        log.info("删除旧设备信息");
        dataService.deleteOldData();
        PageResult pageResult = dataService.page(page, pageSize);
        return Result.success(pageResult);
    }



}